package com.github.binarywang.demo.wx.mp.controller;


import com.github.binarywang.demo.wx.mp.beans.WxMultiConfigDto;
import com.github.binarywang.demo.wx.mp.dto.ClientIDProccessor;
import com.github.binarywang.demo.wx.mp.dto.Result;
import com.github.binarywang.demo.wx.mp.dto.TokenProccessor;
import com.github.binarywang.demo.wx.mp.service.QrService;
import com.github.binarywang.demo.wx.mp.service.wxservice;
import com.github.binarywang.demo.wx.mp.wxService.MulitConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author chi  2018-01-30 11:34
 **/
@Api(tags = "添加公众号模块")
@Controller
@RequestMapping("/api")
public class WxMultiConfigController {

    @Resource
    private MulitConfigService mulitConfigService;

    @Autowired
    private QrService qrService;
    /**
     * 增加或修改微信号配置
     * @param wxMultiConfigDto /wx/portal
     * @return
     */
    @Autowired
    wxservice ws;

    @Autowired
    HttpServletRequest request; //通过注解获取一个request

    @ApiOperation(value="绑定公众号",notes="商户需填写appid、appsecret")
    @ResponseBody
    @ApiImplicitParams({
        @ApiImplicitParam(name="appid",value="公众号id",required = true),
        @ApiImplicitParam(name="appsecret",value="公众号秘钥",required = true),
        @ApiImplicitParam(name="token",value="公众号token",required = false),
        @ApiImplicitParam(name="aes_key",value="公众号aes_key",required = false),
        @ApiImplicitParam(name="hao_zhu_id",value="号主id",required = true),
    })
    @GetMapping("/wx_mulit_config")
    public Result<WxMultiConfigDto> addWxMulitConfig(@RequestParam(value = "hao_zhu_id",required = true)String hao_zhu_id,
                                   @RequestParam(value = "appid",required = true)String appid,
                                   @RequestParam(value = "appsecret",required = true)String appsecret,
                                   @RequestParam(value = "token",required = false)String token,
                                   @RequestParam(value = "aes_key",required = false)String aes_key) {

        if (appid.equals("")){
            return Result.error("公众号appid不能为空");
        }else if (appsecret.equals("")){
            return Result.error("公众号秘钥不能为空");
        }
        WxMultiConfigDto gzh = ws.getGZHbyappid(appid);
        if(gzh!=null){
            return Result.error("公众号已存在");
        }
        WxMultiConfigDto wxMultiConfigDto = new WxMultiConfigDto();//wxMultiConfigDto.getAppid()  wxMultiConfigDto.getAppsecret()
        wxMultiConfigDto.setAppid(appid);
        wxMultiConfigDto.setHao_zhu_id(hao_zhu_id);
        wxMultiConfigDto.setAppsecret(appsecret);
        wxMultiConfigDto.setAes_key(aes_key);
        String client_id=ClientIDProccessor.getInstance().makeClientID();
        wxMultiConfigDto.setClient_id(client_id);
        wxMultiConfigDto.setUrl("https://api.lingsulife.com/wx/portal/"+client_id);
        wxMultiConfigDto.setStatus("available");
        if(token ==null) {
            wxMultiConfigDto.setToken(TokenProccessor.getInstance().makeToken());
        }else wxMultiConfigDto.setToken(token);

        mulitConfigService.generateWxMulitConfig(wxMultiConfigDto);
        ws.addGZ(wxMultiConfigDto);
        Result result=new Result();
        result.setMsg("绑定成功！");
        return Result.success(wxMultiConfigDto);
    }

    @ApiOperation("生成二维码")//
    @ApiImplicitParams({
        @ApiImplicitParam(name="client_id",value="公众号client_id",required = true),
        @ApiImplicitParam(name="merchantsId",value="号主的Long Id",required = true),
    })
    @GetMapping(value = "/generate/{client_id}",produces = MediaType.IMAGE_JPEG_VALUE)
    @ResponseBody
    public byte[] generate(@PathVariable String client_id, @RequestParam("merchantsId")Long merchantsId, HttpServletResponse response){
        String qrUrl = qrService.getQrUrl(client_id, merchantsId);
        if (StringUtils.isNotBlank(qrUrl)){
            try {
                response.sendRedirect(qrUrl);
                return null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return qrService.getUrl(client_id, merchantsId);
    }
}
